import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'


const login = () => import('@/pages/login/login.vue')
const layout = () => import('@/pages/index/layout.vue')
const chart = () => import('@/pages/index/chart.vue')

const menu = () => import('@/pages/menu/index.vue')
const info = () => import('@/pages/menu/info.vue')

const role = () => import('@/pages/role/index.vue')
const roleInfo = () => import('@/pages/role/info.vue')

const admin = () => import('@/pages/admin/index.vue')
const adminInfo = () => import('@/pages/admin/info.vue')

const cate = () => import('@/pages/cate/index.vue')
const cateInfo = () => import('@/pages/cate/info.vue')

const spec = () => import('@/pages/spec/index.vue')
const specInfo = () => import('@/pages/spec/info.vue')

const goods = () => import('@/pages/goods/index.vue')
const goodsInfo = () => import('@/pages/goods/info.vue')

const member = () => import('@/pages/member/index.vue')
const memberInfo = () => import('@/pages/member/info.vue')

const banner = () => import('@/pages/banner/index.vue')
const bannerInfo = () => import('@/pages/banner/info.vue')

const seckill = () => import('@/pages/seckill/index.vue')
const seckillInfo = () => import('@/pages/seckill/info.vue')

Vue.use(Router)

const router =new Router({
  mode: 'history',
  routes: [
    {
      path: '/index',
      name: 'index',
      component: layout,
      children: [
        { path: '', component: chart },
      ]
      // redirect:{
      //   path:'/index'
      // }
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: login
    },
    {
      path: '/',
      name: 'layout',
      meta: {
        title: '首页'
      },
      component: layout,
      children: [
        { path: '', component: chart },
        // 菜单
        { path: 'menu', component: menu },
        { path: 'menu/add', component: info },
        { path: 'menu/:id', component: info },
        // 角色
        { path: 'role', component: role },
        { path: 'role/add', component: roleInfo },
        { path: 'role/:id', component: roleInfo },
        // 管理员
        { path: 'admin', component: admin },
        { path: 'admin/add', component: adminInfo },
        { path: 'admin/:uid', component: adminInfo },
        // 商品分类
        { path: 'category', component: cate },
        // 商品规格
        { path: 'specs', component: spec },
        // 商品管理
        { path: 'goods', component: goods },
        // 会员管理
        { path: 'member', component: member },
        // 轮播图管理
        { path: 'banner', component: banner },
        // 秒杀活动
        { path: 'seckill', component: seckill },
      ],
    },
  ]
})
// 解决:NavigationDuplicated: Avoided redundant navigation to current location.捕获错误
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to, from, next) => {
  // 判断路由
  if (to.path === '/login') {
    // 登录页不做拦截
    next();
  } else {
    if (localStorage.getItem('userInfo')) {
      // localstorage有数据，说明已经登录过
      next()
    } else {
      // localstorage没有数据，说明没有登录，就去登录
      next('/login')
      query: {redirect: to.fullPath}
    }
  }
})
export default router

